package com.abc.recycling.controller.admin;

import com.abc.recycling.model.RecyclingStation;
import com.abc.recycling.service.RecyclingStationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/admin/stations")
@RequiredArgsConstructor
public class AdminRecyclingStationController {

    private final RecyclingStationService stationService;

    @GetMapping
    public String getStationsPage(Model model, @RequestParam(required = false) String keyword) {
        try {
            log.debug("获取回收站列表页面, keyword={}", keyword);
            List<RecyclingStation> stations = stationService.findStations(keyword);
            model.addAttribute("stations", stations);
            model.addAttribute("keyword", keyword);
            return "admin/station/list";
        } catch (Exception e) {
            log.error("获取回收站列表页面失败: {}", e.getMessage());
            throw e;
        }
    }

    @GetMapping("/{id}")
    @ResponseBody
    public RecyclingStation getStation(@PathVariable Long id) {
        try {
            log.debug("获取回收站信息, id={}", id);
            return stationService.findById(id);
        } catch (Exception e) {
            log.error("获取回收站信息失败: {}", e.getMessage());
            throw e;
        }
    }

    @PostMapping
    @ResponseBody
    public void createStation(@RequestBody RecyclingStation station) {
        try {
            log.info("创建回收站");
            stationService.create(station);
            log.info("回收站创建成功");
        } catch (Exception e) {
            log.error("创建回收站失败: {}", e.getMessage());
            throw e;
        }
    }

    @PutMapping("/{id}")
    @ResponseBody
    public void updateStation(@PathVariable Long id, @RequestBody RecyclingStation station) {
        try {
            log.info("更新回收站信息, id={}", id);
            station.setId(id);
            stationService.update(station);
            log.info("回收站信息更新成功");
        } catch (Exception e) {
            log.error("更新回收站信息失败: {}", e.getMessage());
            throw e;
        }
    }

    @PostMapping("/{id}/status")
    @ResponseBody
    public void updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            log.info("更新回收站状态, id={}, status={}", id, status);
            stationService.updateStatus(id, status);
            log.info("回收站状态更新成功");
        } catch (Exception e) {
            log.error("更新回收站状态失败: {}", e.getMessage());
            throw e;
        }
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public void deleteStation(@PathVariable Long id) {
        try {
            log.info("删除回收站, id={}", id);
            stationService.delete(id);
            log.info("回收站删除成功");
        } catch (Exception e) {
            log.error("删除回收站失败: {}", e.getMessage());
            throw e;
        }
    }
} 